﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace wpStatusDocuments.wbStatusDocuments
{
    [ToolboxItemAttribute(false)]
    public class wbStatusDocuments : WebPart
    {
        Label lStatus;
        Button bNaUtv;
        Button bUtv;

        protected override void CreateChildControls()
        {
            string vText = SPContext.Current.RootFolderUrl;
            SPList list = SPContext.Current.List;
            String vWhere = "<Where><Eq><FieldRef Name=\"FileRef\"/><Value Type=\"Text\">" + vText + "</Value></Eq></Where>";
            SPListItemCollection mItems = RetItemQuery(list, vWhere);
            if (mItems != null)
            {
                if (mItems.Count == 1)
                {
                    foreach (SPListItem expiredItem in mItems)
                    {
                        lStatus = new Label();
                        lStatus.Text = "Статус : " + expiredItem["Состояние утверждения"].ToString() + " ";
                        this.Controls.Add(lStatus);

                        if (expiredItem["Поставщик"] != null)
                        {
                            if (expiredItem["Состояние утверждения"].ToString() == "Черновик")
                            {
                                //--
                                bNaUtv = new Button();
                                bNaUtv.Text = "Отправить на утверждение";
                                bNaUtv.Click += new EventHandler(btn_ClickNaUtv);
                                this.Controls.Add(bNaUtv);
                                //--
                                if (
                                    System.Web.HttpContext.Current.User.IsInRole(@"KORABLIK\DTZ_uchet") 
                                    || 
                                    System.Web.HttpContext.Current.User.IsInRole(@"KORABLIK\Отдел учета ДТЗ")
                                    )
                                {
                                    bUtv = new Button();
                                    bUtv.Text = "Утвердить";
                                    bUtv.Click += new EventHandler(btn_ClickUtv);
                                    this.Controls.Add(bUtv);
                                }
                                //--
                            }
                            if (expiredItem["Состояние утверждения"].ToString() == "Ожидают утверждения")
                            {
                                //--
                                if (System.Web.HttpContext.Current.User.IsInRole(@"KORABLIK\DTZ_uchet") || System.Web.HttpContext.Current.User.IsInRole(@"KORABLIK\Отдел учета ДТЗ"))
                                {
                                    bUtv = new Button();
                                    bUtv.Text = "Утвердить";
                                    bUtv.Click += new EventHandler(btn_ClickUtv);
                                    this.Controls.Add(bUtv);
                                }
                                //--
                            }
                        }
                    }
                }
            }
        }

        SPListItemCollection RetItemQuery(SPList mList, String mWhere)
        {
            SPListItemCollection mItem = null;
            SPQuery mQuery = new SPQuery();
            mQuery.Query = mWhere;
            mQuery.ViewAttributes = "Scope=\"RecursiveAll\" ModerationType=\"Moderator\"";
            mItem = mList.GetItems(mQuery);
            return mItem;
        }

        void btn_ClickNaUtv(object sender, EventArgs e)
        {
            lStatus.Text = "Статус: Ожидают утверждения";
            this.Controls.Remove(bNaUtv);
            //--
            string vText = SPContext.Current.RootFolderUrl;
            SPList list = SPContext.Current.List;
            String vWhere = "<Where><Eq><FieldRef Name=\"FileRef\"/><Value Type=\"Text\">" + vText + "</Value></Eq></Where>";
            SPListItemCollection mItems = RetItemQuery(list, vWhere);
            if (mItems != null)
            {
                if (mItems.Count == 1)
                {
                    foreach (SPListItem expiredItem in mItems)
                    {
                        if (expiredItem["Поставщик"] != null)
                        {
                            expiredItem["Состояние утверждения"] = "Ожидают утверждения";
                            expiredItem.Update();
                        }
                    }
                }
            }
        }
        void btn_ClickUtv(object sender, EventArgs e)
        {
            lStatus.Text = "Статус: Утвержден";
            this.Controls.Remove(bUtv);
            this.Controls.Remove(bNaUtv);
            //--
            string vText = SPContext.Current.RootFolderUrl;
            SPList list = SPContext.Current.List;
            String vWhere = "<Where><Eq><FieldRef Name=\"FileRef\"/><Value Type=\"Text\">" + vText + "</Value></Eq></Where>";
            SPListItemCollection mItems = RetItemQuery(list, vWhere);
            if (mItems != null)
            {
                if (mItems.Count == 1)
                {
                    foreach (SPListItem expiredItem in mItems)
                    {
                        if (expiredItem["Поставщик"] != null)
                        {
                            expiredItem["Состояние утверждения"] = "Утвержден";
                            expiredItem.Update();
                        }
                    }
                }
            }
        }


    }
}

